package com.yun.space.pro.aHref.actiom;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.yun.space.base.BaseAction;
import com.yun.space.entity.AHerf;

/**
 * 链接ction
 * @author guo
 *
 */
public class AHrefAction extends BaseAction<AHerf>{
	private File file;//文件
	private String fileFileName;//文件名称 
	private String fileContentType; //文件类型 
	private String savePath;//文件名
	/*
	 * 	@guo 
	 *  加载后台顶部轮播图修改页面
	 */
	public String toEditSurveillanceSystem() {
		ActionContext.getContext().getValueStack().set("AHerf",aHerfService.getById(getModel().getId()));
		return "toEditSurveillanceSystem";
	}
	//友情连接修改
	public String toEditFriendshipLinks() {
		ActionContext.getContext().getValueStack().set("AHerf",aHerfService.getById(getModel().getId()));
		return "toEditFriendshipLinks";
	}
	public String editSurveillanceSystem() {
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getModel().setType(2);
		getModel().setPicture(getFileFileName());
		aHerfService.update(getModel());
		ActionContext.getContext().getValueStack().set("AHerfs",aHerfService.findByType(2));
		return "editSurveillanceSystem";
	}
	//友情连接修改
	public String editFriendshipLinks() {
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getModel().setType(1);
		getModel().setPicture(getFileFileName());
		aHerfService.update(getModel());
		ActionContext.getContext().getValueStack().set("AHerfs",aHerfService.findByType(1));
		return "editFriendshipLinks";
	}
	/*
	 * 	@guo 
	 *  加载集成监控系统添加页面
	 */
	public String toAddSurveillanceSystem() {
		return "toAddSurveillanceSystem";
	}
	//加载友情连接添加页面
	public String toAddFriendshipLinks() {
		return "toAddFriendshipLinks";
	}
	/*
	 * 	@guo 
	 * 添加集成系统
	 */
	public String addSurveillanceSystem() {
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getModel().setType(2);
		getModel().setPicture(getFileFileName());
		aHerfService.save(getModel());
		ActionContext.getContext().getValueStack().set("AHerfs",aHerfService.findByType(2));
		return "addSurveillanceSystem";
	}
	//友情连接添加
	public String addFriendshipLinks() {
		try {
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName());
			FileInputStream fis = new FileInputStream(getFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
			fis.close();
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		getModel().setType(1);
		getModel().setPicture(getFileFileName());
		aHerfService.save(getModel());
		ActionContext.getContext().getValueStack().set("AHerfs",aHerfService.findByType(1));
		return "addFriendshipLinks";
	}
	// 集中监控系统
	public String delectSurveillanceSystem() {
		aHerfService.delete(getModel().getId());
		ActionContext.getContext().getValueStack().set("AHerfs",aHerfService.findByType(2));
		return "delectSurveillanceSystem";
	}
	//删除友情连接
	public String delectFriendshipLinks() {
		aHerfService.delete(getModel().getId());
		ActionContext.getContext().getValueStack().set("AHerfs",aHerfService.findByType(1));
		return "delectFriendshipLinks";
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
}
